package lmh.example.exam3_1;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class MainActivity extends Activity
{
    private TextView show;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        show = findViewById(R.id.show);
        new Thread()
        {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void run()
            {
                try(
                        // 建立连接到远程服务器的Socket
                        Socket socket = new Socket("192.168.116.2", 5568);  // ①
                        // 将Socket对应的输入流包装成BufferedReader
                        BufferedReader br = new BufferedReader(
                                new InputStreamReader(socket.getInputStream())))
                {
                    // 进行普通IO操作
                    String line = br.readLine();
                    show.setText("来自服务器的数据：" + line);
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
